% Plots figures
set(0,'DefaultFigureWindowStyle','docked')  % docks all figures

figure(1)
subplot(1,2,1); plot(Pgrid,D); xlabel('p'); ylabel('D');
subplot(1,2,2); plot(-Pdistans,D); xlabel('x'); ylabel('D');

figure(2)
subplot(1,2,1); plot(Pgrid,cumsum(Pdist_sh)); xlabel('p'); ylabel('CDF (after shocks)');
subplot(1,2,2); plot(-Pdistans,cumsum(Pdist_sh)); xlabel('x'); ylabel('CDF (after shocks)');

figure(3)
subplot(1,2,1); plot(Pgrid,cumsum(Pdist)); xlabel('p'); ylabel('CDF (after adjustment)');
subplot(1,2,2); plot(-Pdistans,cumsum(Pdist)); xlabel('x'); ylabel('CDF (after adjustment)');

%figure(4)
%plot(-Pdistans,f_sh); xlabel('x'); ylabel('pdf');

figure(5)
plot(-Pdistans,lambda); xlabel('x'); ylabel('$\lambda$');

figure(10)
colormap([0.73 0.83 0.96])
%bar(price_ch,dens,1,'EdgeColor','none');  
hold on
if length(Pchangegrid)==length(denspchanges)
%  bar(Pchangegrid,denspchanges);   
  %bar(std_ref_price_data,std_ref_price_density_data);
  stand_pc_dist_model = interp1((Pchangegrid-EPchange)/StdPchanges,denspchanges,std_ref_price_data,'linear','extrap');
  stand_pc_dist_model = stand_pc_dist_model/sum(stand_pc_dist_model);  %make sure it integrates to 1
  plot(std_ref_price_data,stand_pc_dist_model, std_ref_price_data,std_ref_price_density_data);   
end
title('Distribution of standardized non-zero price changes')
xlabel('Size of price changes')
ylabel('Density of price changes')
%xlim([-0.55 0.55])
legend('model','data');
xlim([-3 3])

figure(14)
%map=colormap;
%colormap([1 1 1; map])
%plot(-Pdistans,Pdist_sh,gap_data,density_data)
gap_density_model = interp1(-Pdistans,Pdist_sh,deviation_data,'linear','extrap');
gap_density_model = gap_density_model/sum(gap_density_model);  %make sure it integrates to 1
plot(deviation_data,gap_density_model,deviation_data,density_data)
xlabel('Log relative price, gap')
title('Density of firms after quality shocks and inflation')
legend('f model','f data')
axis tight
zl=zlim;
%zlim(zl)

figure(15)
%map=colormap;
%colormap([1 1 1; map])
plot(Pgrid,(1-lambda).*Pdist_sh)
xlabel('Log relative price')
title('Density of non-adjusting firms')
axis tight
zlim(zl)

figure(16)
%map=colormap;
%colormap([1 1 1; map])
plot(Pgrid,lambda.*Pdist_sh)
xlabel('Log relative price')
title('Density of adjusting firms')
axis tight

figure(17)
%map=colormap;
%colormap([1 1 1; map])
%plot(Pgrid,lambda,Pgrid,lambda_prime)
lambda_prime_model = interp1(-Pdistans,lambda_prime,deviation_data,'linear','extrap');
plot(deviation_data,lambda_prime_model,deviation_data,freq_data)
xlabel('Gap')
title('Adjustment probability function')
legend('$\lambda$'' (model)','$\lambda$'' (data)')
axis tight

figure(18)
%map=colormap;
%colormap([1 1 1; map])
%plot(Pgrid,lambda,Pgrid,lambda_prime)
size_prime_model = interp1(Pdistans,size_prime,deviation_data,'linear','extrap');
plot(deviation_data,size_prime_model,deviation_data,size_data)
xlabel('Gap')
title('Adjustment probability function')
legend('size'' (model)','size'' (data)')
axis tight

%standardized gap
figure(19)
%map=colormap;
%colormap([1 1 1; map])
mean_gap_model = Pdist_sh'*(-Pdistans);
std_gap_model = sqrt(Pdist_sh'*(-Pdistans-mean_gap_model).^2);
std_gap_density_model = interp1(-Pdistans./std_gap_model,Pdist_sh,std_gap_data,'linear','extrap');
higherabs3_ind=find(abs(-Pdistans./std_gap_model)>=3);
std_gap_density_model = std_gap_density_model/(sum(std_gap_density_model)/(1-sum(Pdist_sh(higherabs3_ind))));  %make sure it integrates to 1-those outside 3
plot(std_gap_data,std_gap_density_model,std_gap_data,std_gap_density_data)
xlabel('Standardized gap')
title('Density of firms after quality shocks and inflation')
legend('f model','f data')
axis tight
zl=zlim;
%zlim(zl)

for ii=1:24
figure(20);
plot(lambdaprime(:,ii)); hold on;
end

figure(21);
%map=colormap;
%colormap([1 1 1; map])
%plot(Pgrid,lambda,Pgrid,lambda_prime)
lambda_model = interp1(-Pdistans,lambda,deviation_data,'linear','extrap');
lambda_prime_h1_model = interp1(-Pdistans,lambdaprime(:,1),deviation_data,'linear','extrap');
lambda_prime_h3_model = interp1(-Pdistans,lambdaprime(:,3),deviation_data,'linear','extrap');
lambda_prime_h12_model = interp1(-Pdistans,lambdaprime(:,12),deviation_data,'linear','extrap');
lambda_prime_h24_model = interp1(-Pdistans,lambdaprime(:,24),deviation_data,'linear','extrap');
lambda_prime_h12_data  = min(freq_any_h12_mean_data,1);
lambda_prime_h24_data  = min(freq_any_h24_mean_data,1);

plot(deviation_data,[lambda_prime_h12_model lambda_prime_h24_model lambda_prime_h12_data lambda_prime_h24_data])
xlabel('Gap')
title('Adjustment probability function')
legend('$\lambda$'' (h=12, model)','$\lambda$'' (h=24, model)','$\lambda$'' (h=12, data)','$\lambda$'' (h=24, data)')
axis tight



%write the data to xls file
write_data;
